<?php
include("inc/head.php");

$MID = $_GET['MID'];

$Mould = new Mould(array("MID"=>0));
$moulds = $Mould->allchildren();
if(is_numeric($MID))
	$Mould->getObjById($MID, "MID", "Mould");

if($_POST['doPost'] == "true")
{	
	
	$Mould = new Mould($_POST);
	if(is_numeric($MID) && $MID > 0)
		$req = $Mould->update("Mould", "MName,MUrl,PID,MInfo,Menu_id,Menu_name", "MID");
	else
	{
		$Mould->AddTime = time();
		$Mould->UID = isset($_SESSION['UID'])?$_SESSION['UID']:0;
		$req = $Mould->add("Mould", "MName,MUrl,PID,MInfo,UID,AddTime,Menu_id,Menu_name", "MID");
	}
	
	//note 左上角和右上角菜单
	$sql = "SELECT MID, MUrl, Menu_id, Menu_name FROM Mould WHERE Menu_id = -1 OR Menu_id = -2";
	$left_right = $db->getAll( $sql );
	$menu = array();
	foreach( $left_right as $v ){
		$menu[ abs($v['Menu_id'])][] = $v;
	}
	file_put_contents('./image2/cache/menu_cache_2.php', serialize( $menu ));
	
	
	if($req)
	{
		new ActionLog(array("LogType"=>"mould_success","Action"=>"{$MID},{$Mould->MID}"));
		alert("sucess","location","mould.php?MID=$MID");
	}
	else
	{
		new ActionLog(array("LogType"=>"mould_err","Action"=>"{$MID},{$Mould->MID}"));
		alert("err","location","mould.php?MID=$MID");
	}
	
	exit;
}

//note 取得所有显示位置，即一二级菜单
$sql = "SELECT * FROM menu ORDER BY sort ASC";
$menu = $db->getAll( $sql );

$menu_arr = $list = array();
foreach( $menu as $v ){
	$menu_arr[$v['pid']][$v['mid']] = $v;
	$list[$v['mid']] = $v['name'];
}

//note 各模块显示位置
$pos = array();
foreach( $menu as $v ){
	$pos[$v['mid']] = $list[$v['pid']].' > '.$v['name'];
}
//note 左上角和右上角菜单
$sql = "SELECT MID, MUrl, Menu_id, Menu_name FROM Mould WHERE Menu_id = -1 OR Menu_id = -2";
$left_right = $db->getAll( $sql );
foreach( $left_right as $v ){
	if( $v['Menu_id'] == -1 ) $pos[$v['Menu_id']] = '左上角'; else $pos[$v['Menu_id']] = '右上角';
}


$list = "";
$typeVal[] = 0;
$typeKey[] = '根';

for($i = 0; $i < count($moulds); $i++)
{
	if($moulds[$i]->PID == 0)
	{
		$attr = "|-";
		$showInList = true;
	}
	else
	{
		if($moulds[$i]->PID == $moulds[$i-1]->MID)
			$attr = "　".$attr;
	}
	if($moulds[$i]->MID == $Mould->MID)
	{
		$showInList = false;
	}
	if($showInList)
	{
		$typeVal[] = $moulds[$i]->MID;
		$typeKey[] = $attr.$moulds[$i]->MName;
	}
	if($Mould->MID == $moulds[$i]->MID)
		$list .= "<tr><td>$attr".$moulds[$i]->MName."</td><td>-->&nbsp;&nbsp;&nbsp;".$pos[$moulds[$i]->Menu_id]."</td></tr>";
	else
		$list .= "<tr><td>$attr<a href='?MID=".$moulds[$i]->MID."'>".$moulds[$i]->MName."</a></td><td>-->&nbsp;&nbsp;&nbsp;".$pos[$moulds[$i]->Menu_id]."</td></tr>";
}

?>
<script>
var templates = new Array();
<?php
$sql = "select * from Template order by TID desc";
$result = $db->query($sql);
while($row = $db->fetch_array($result))
{
	$selected = '';
	if($row['TID'] == $Mould->TID)
		$selected = ' selected="selected"';
	$selectOption .= "<option value=\"{$row[TID]}\"$selected>{$row[TName]}</option>";
	echo "templates[{$row[TID]}]=new Array('".addslashes($row[TName])."','".BIN_URL."/docList_{$row[TID]}.php');\n";
}
?>
function change(i)
{
	document.form1.MName.value = templates[i][0];
	document.form1.MUrl.value = templates[i][1];
}

//note 新增模块显示菜单
function addMenu(){
	parent.art.dialog({id:"a",title:"新增菜单",iframe:"./bg/menu_add.php",width:"500px",height:"150px"});
}

//note 编辑模块显示菜单
function editMenu(){
	parent.art.dialog({id:"a",title:"编辑菜单",iframe:"./bg/menu_edit.php",width:"700px",height:"500px"});
}
</script>
<form method="post" name="form1">
<input type="hidden" value="true" name="doPost">
<input type="hidden" value="<?php echo $MID; ?>" name="MID">

<table width="90%" cellpadding="1" cellspacing="1" class="list-div">
	<tr>
		<td width="100">项目</td>
		<td>值</td>
	</tr>
	<tr>
		<td>模块名称</td>
		<td><input class="input-text" size="25" type="text" name="MName" value="<?php echo $Mould->MName; ?>">  <select name="TID" onchange="change(this.options[this.selectedIndex].value)"><option>--选择模块--</option><?php echo $selectOption; ?></select></td>
	</tr>
	<tr>
		<td>模块地址</td>
		<td><input class="input-text" size="60" type="text" name="MUrl" value="<?php echo $Mould->MUrl; ?>"></td>
	</tr>
	<tr>
		<td>父模块</td>
		<td><select name="PID">
		<?php
		for($i = 0; $i < count($typeVal); $i++)
		{
			echo "<option value=\"{$typeVal[$i]}\"";
			if($Mould->PID == $typeVal[$i])
				echo " selected=\"selected\"";
			echo ">{$typeKey[$i]}</option>\n";
		}
		?>
		</select>
		</td>
	</tr>
	<tr>
		<td>模块显示位置</td>
        <td>
      <select name="Menu_id" id="menu_id">
      		<option value="">--选择菜单--</option>
            <option value="-1" <?php if( $Mould->Menu_id == -1 ){ ?>selected="selected" <?php }?>>左上角</option>
            <option value="-2" <?php if( $Mould->Menu_id == -2 ){ ?>selected="selected" <?php }?>>右上角</option>
          	<option value="0" <?php if( $Mould->Menu_id == 0 ){ ?>selected="selected" <?php }?>>根菜单</option>
           <?php foreach( $menu_arr[0] as $v){?>
            <option value="<?php echo $v['mid'];?>" <?php if( $v['mid'] == $Mould->Menu_id ){ ?>selected="selected" <?php }?>><?php echo $v['name'];?></option>
				<?php if( !empty($menu_arr[$v['mid']]) ){ foreach( $menu_arr[$v['mid']] as $v2){?>
                <option value="<?php echo $v2['mid'];?>" <?php if( $v2['mid'] == $Mould->Menu_id ){ ?>selected="selected" <?php }?>>&nbsp;&nbsp;&nbsp;<?php echo $v2['name'];?></option>
            <?php }}}?>
            </select>
      <a href="javascript:addMenu();"><img src="./bg/frame/images/system/add.gif" title="新增模块显示位置" style="position:relative;top:5px;"/></a>
      <a href="javascript:editMenu();"><img src="./bg/frame/images/system/edit.gif" title="编辑模块显示位置" style="position:relative;top:5px;"/></a>
      </td>
	</tr>
    <tr>
		<td>模块显示名称</td>
	  <td><input class="input-text" size="25" type="text" name="Menu_name" value="<?php echo $Mould->Menu_name; ?>"></td>
	</tr>
    <tr>
		<td valign="top">说明</td>
	  <td><textarea name="MInfo" cols="100" rows="5"><?php echo htmlspecialchars($Mould->MInfo); ?></textarea></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="sub" value="提交"></td>
	</tr>
</table>

<!--列表开始-->
<table><?php
echo $list;
?></table>
<!--列表结束-->

</form>
<?php
include("inc/foot.php");
?>
